package com.lin.controller.admin;

import com.lin.commons.Result;
import com.lin.entity.User;
import com.lin.service.AdminService;
import com.lin.vo.DataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Created by Lin Weihong
 * @Time on 2021/11/24 19:24
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/user")
@Slf4j
public class AdminUserController {

    @Autowired
    AdminService adminService;

    /**
     * 管理员查询所有用户
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/userList")
    public DataVO<User> userList(Integer page, Integer limit) {
        return adminService.findData(page, limit);
    }

    /**
     * 管理员添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        int flag = adminService.addUser(user);
        log.info("*****insert的用户: " + user);
        if (flag == 1) {
            return Result.success();
        } else {
            return Result.failure("用户名:" + user.getUserName() + "已经存在，请重新输入");
        }
    }

    /**
     * 管理员删除用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/delete/{userId}")
    public Result deleteUser(@PathVariable("userId") Integer userId) {
        boolean remove = adminService.removeById(userId);
        if (remove) {
            return Result.success();
        } else {
            return Result.failure();
        }
    }

    /**
     * 管理员更新用户
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        boolean update = adminService.updateUser(user);
        if (update) {
            return Result.success();
        } else {
            return Result.failure();
        }
    }


}
